//
//  DUTypeManagerController.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/3.
//

import Cocoa

class DUTypeManagerController: NSViewController {
    
    @IBOutlet weak var calView: NSTabView!
    @IBOutlet weak var addButton: DUImageButton!
    
    private var calBox:DUYearCalView!
    private var buttons = RadioGroup<DUButton>()
    
    private var add = DUAddPopPanel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initialize()
    }
    
    private func initialize () {
        let t1 = DUHolidayTabViewItem()
        t1.view = createTabBox()
        calView.addTabViewItem(t1)
        
        let t2 = DUHolidayTabViewItem()
        t2.view = createTabHoi()
        calView.addTabViewItem(t2)
        
        
    }
    
    private func createTabHoi () -> NSView {
        let view = NSView(frame: NSMakeRect(0, 0, 720, 436))
        //view.layer?.backgroundColor = NSColor.blue.cgColor
        
        let b = NSButton(frame: NSMakeRect(100, 100, 100, 50))
        b.stringValue = "测试按钮"
        view.addSubview(b)
        return view
    }
    
    private func createTabBox () -> NSView {
        let view = NSView(frame: NSMakeRect(0, 0, 720, 436))
        let cy = CalendarUtil.getCurrentYear()
        var year = cy + 1
        for i in (0...5) {
            let x = i*95 + 30
            let b = DUButton(frame: NSRect(x: x, y: 400, width: 90, height: 30))
            b.stringValue("\(year)", data: 1)
            let p = PageTypeModel(pageType: .MONTH, yearStart: year, yearEnd: 0, month: 0)
            b.target = p
            if year == cy {
                b.defaultSelected()
            }
            buttons.addSubview(view: b)
            view.addSubview(b)
            year -= 1
        }
        buttons.btnChange = {(params:NSObject?) in
            if params != nil {
                let b = params as! DUButton
                self.calBox.update(year: b.target!.yearStart)
            }
        }
        
        view.addSubview(createTabCal())
        return view
    }
    
    private func createTabCal () -> NSScrollView {
        let scrollView = NSScrollView(frame: NSMakeRect(0, 0, 720, 400))
        scrollView.drawsBackground = false
        scrollView.hasHorizontalScroller = false
        scrollView.hasVerticalScroller = true
        scrollView.autohidesScrollers = true
        
        calBox = DUYearCalView(frame: NSMakeRect(0, 0, 680, 1000))
        calBox.update(year: CalendarUtil.getCurrentYear())
        let d = DUClipView()
        d.drawsBackground = false
        d.documentView = calBox
        scrollView.contentView = d
        return scrollView
    }
    
    @IBAction func radioClick(_ sender: NSSegmentedControl) {
        calView.selectTabViewItem(at: sender.selectedSegment)
    }
    
    @IBAction func addButton(_ sender: Any) {
        GlobalControl.shared.system?.enableAll(e: true)
        NSApplication.shared.runModal(for: add.window!)
    }
    
    var isEnable:Bool = false {
        didSet {
            buttons.isEnable = isEnable
            addButton.isEnabled = isEnable
        }
    }
    
    
}
